<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class potongan extends CI_Controller {
    public function __construct() {
        parent::__construct();

        $this->load->library('auth');
        $this->load->library('base');
        $this->load->library('ajaxresponse');
        $this->load->library('datatables');
    }

    function index() {
        $this->auth->is_logged_in('form/login', 2);
        $this->base->head_html('Data Potongan');
        $this->load->view('br-03/potongan');
    }

    function create() {
        $this->auth->is_logged_in('form/login', 2);
        $this->base->head_html('Buat Potongan');
        $this->load->view('pengurus/br-03/buat_potongan');
    }

    function date($date)
    {
        $this->auth->is_logged_in('form/login', 2);
        $this->response = new ajaxresponse();

        $this->load->model('crud/m_angsuran');
        $year = date("Y", strtotime($date));
        $month = date("m", strtotime($date));
        $result = $this->m_angsuran->get("MONTH(TGL_ANGSURAN) = $month && YEAR(TGL_ANGSURAN) = $year");

        $this->response->data = $result->result();
        echo json_encode($this->response);
    }

    function id($id)
    {
        $this->auth->is_logged_in('form/login', 2);
        $this->response = new ajaxresponse();

        $this->load->model('crud/m_angsuran');
        $result = $this->m_angsuran->get("ID_PINJAMAN = $id");

        $this->response->data = $result->result();
        echo json_encode($this->response);
    }

    function realisasi() {
        $this->auth->is_logged_in('form/login', 2);
        $this->base->head_html('Realisasi Potongan');
        $this->load->view('pengurus/br-03/realisasi_potongan');
    }

    function pelunasan(){
        $this->load->model('crud/m_realiasai_angsuran');
        $result = $this->m_realisasi_angsuran->insert();
        $this->response->data = $result->result();
        echo json_encode($this->response);
    }

    function realize(){
        $this->auth->is_logged_in('form/login', 2);
        $this->response = new ajaxresponse();

        //$this->response->data = $result->result();
        echo json_encode($this->response);
    }

    function createpotongan(){
        $this->auth->is_logged_in('form/login', 2);
        $this->base->head_html('Buat Potongan');
        $this->load->view('pengurus/br-03/create_potongan');        
    }

    function generate_potongan(){
        $this->auth->is_logged_in('form/login', 2);

        $this->load->model('crud/m_pinjaman', "", TRUE);
        $this->load->model('palapa_client/m_pinjaman_palapa', "", TRUE);

        $this->response = new ajaxresponse();
//        $this->response->data = $this->m_pinjaman->generate_potongan($date);
        $this->response->data = $this->m_pinjaman->generate_potongan();

        $result = $this->m_pinjaman->get_potongan_changed();
        $this->response->messages[] = $result->num_rows();
        foreach($result->result() as $row)
            $this->m_pinjaman_palapa->update_piutang($row->ID_PINJAMAN, $row->PIUTANG, $row->POTONGAN, $row->MARGIN);

        echo json_encode($this->response);
    }

}